var ie = document.all;
var ns6 = document.getElementById && !document.all;
var isMenu = false;
var menuSelObj = null;
var overpopupmenu = false;
var blank = 1;
var fe_solfin;
var ls_tp = [];

function contenedorbodyload(contenedor_caller, mobil, transa, id_frawor) {
//    alinearcss(mobil);
    do_onchange();
    if (contenedor_caller == 1)
        cerrar_transa(transa, id_frawor);
    documentofocus();
    acomodadorcss();
    agregar_onclick_link(mobil);
}

function mouseSelect(e) {
    var obj = ns6 ? e.target.parentNode : event.srcElement.parentElement;
    if (isMenu) {
        if (overpopupmenu == false) {
            isMenu = false;
            overpopupmenu = false;
            document.getElementById('context_menu').style.display = "none";
            return true;
        }
        return true;
    }
    return true;
}

// POP UP MENU
function	ItemSelMenu(e, ele) {
    var obj = ns6 ? e.target.parentNode : event.srcElement.parentElement;
    menuSelObj = obj;
    if (ns6) {
        document.getElementById('context_menu').style.left = e.clientX + document.body.scrollLeft;
        document.getElementById('context_menu').style.top = e.clientY + document.body.scrollTop;
    } else {
        document.getElementById('context_menu').style.pixelLeft = event.clientX + document.body.scrollLeft;
        document.getElementById('menudiv').style.pixelTop = event.clientY + document.body.scrollTop;
    }
    document.getElementById('context_menu').style.display = "";
    //document.getElementById('context_menu_menu1').style.backgroundColor='#FFFFFF';
    document.getElementById('context_menu_menu1_a').href = 'JAVASCRIPT:VOID(0)';
    var msg = ele.getAttribute("onclick"); //iqaro_tabla('1,1,4,2,2,3,3,4','48','1','1','17452','41');return false;
    console.log("valor del elemento[" + msg + "]");
    console.log("valor del elemento[" + msg.substring(0, msg.length - 15) + "]");
    console.log("valor del elemento[" + msg.substring(msg.length - 15, msg.length - 1) + "]");
    msg = msg.substring(0, msg.length - 15); //1,4,2,2,3,3,4','48','1','1','17452','41','_blank2');document.getElementById('context_menu').style.display = 'none';return false;
    msg = msg + ",'_blank" + blank + "');document.getElementById('context_menu').style.display = 'none';return false;"
    document.getElementById('context_menu_menu1_a').setAttribute("onclick", msg);
    isMenu = true;
    blank = blank + 1;
    return false;
}

var zEle, xthf0 = [];

var o_cabeceras = new Array();
var o_tabla_f = new Array();
var o_tabla_t = new Array();
var o_tabla_i = new Array();
var o_tabla_s = new Array();

function do_onchange() {
    var I = navigator.userAgent;
//                console.log("length: " + o_tabla_f.length);
    for (var t = 0; t < o_tabla_f.length; t++) {
        var m_t = document.getElementById(o_tabla_f[t]);
        var super_items = getElementsByClassName(m_t, "q");
        for (var o = 0; o < super_items.length; o++) {
            var obj = super_items[o];
            if (obj.getAttribute("onchange")) {
                if (obj.className == "q f") {//accion para fechas
                    obj = $D.g_ID(obj.getAttribute("id"));
                    obj.onchange();
                }
                if (obj.className == "q r") {//accion para fechas
                    try {
                        if (obj.value != '')
                            obj.onchange();
                    } catch (ep) {/*alert("error1:"+ep);*/
                    }
                }
                else if (obj.className == "input2 q c" | obj.className == "input2 q c2") {//accion para combos
                    try {
                        if (obj.options[obj.selectedIndex].value != '') {
                            obj.onchange();
                        } else if (obj.getAttribute("valpag") != '') {
                            if (obj.options != "undefined") {
                                for (var i = 0; i < obj.options.length; i++) {
                                    if (obj.options[i].value == obj.getAttribute("valpag")) {
                                        obj.options[i].selected = true;
                                        i = obj.options.length - 1;
                                    }
                                }
                                obj.onchange();
                            }
                        } else {
                        }
                    } catch (ep) {/*alert("error2:"+ep);*/
                    }
                }
                else if (obj.tagName == "LABEL") {//accion para un label <dato edl caso >
                    try {
                        if (obj.options[obj.selectedIndex].value != '') {
                            obj.onchange();
                        }
                    } catch (ep) {/*alert("error3:"+ep);*/
                    }
                }
            }
        }
    }

    try {
        //alert("history.length:"+history.length);
        if (history.length <= 1) {
            $D.g_ID("td_back").style.display = "none";
        }
    } catch (e) {
    }

}

function cerrar_transa(transa, id_frawor) {
//                console.log("transa: " + JSON.stringify(ls_tp));
    if (JSON.stringify(ls_tp) != "[]" && JSON.stringify(ls_tp) != null) {
        $D.getHTML($D.base() + "/" + 'wfp?xact=4&transa=' + transa + '&id_frawor=' + id_frawor + '&fe_solfin=' + new Date().getTime() + '&fe_pagina=' + JSON.stringify(ls_tp));
//        gethtmlfromurl(new getXMLObject(), "http://" + window.location.hostname + ":" + window.location.port + "/" + window.location.pathname.split('/')[1] + "/" + 'wfp?xact=4&transa=' + transa + '&id_frawor=' + id_frawor + '&fe_solfin=' + new Date().getTime() + '&fe_pagina=' + JSON.stringify(ls_tp));
    }

}

function alinearcss(m) {
    for (var i = 0; i < o_tabla_t.length; i++) {
//        var filasA = document.getElementById(o_tabla_t[i]).getElementsByTagName("tr");
//        var filasA = document.getElementById(o_tabla_t[i]).tHead.rows;
        var filasA = document.getElementById(o_tabla_t[i]).rows;
//        var columnasA2 = filasA[2].getElementsByTagName("td");
        var columnasA2 = filasA[2].cells;
//        var columnasB2 = filasA[3].getElementsByTagName("td");
        var columnasB2 = filasA[3].cells;
        for (var i2 = 0; i2 < columnasA2.length; i2++) {
            var col = columnasB2[i2];
            columnasA2[i2].style.width = col.offsetWidth + "px";
        }
    }
//    console.log("evaluando m:" + m + "::[" + (m ? "a" : "b") + "]");
    if (!m) {

        xAddEventListener(window, 'load',
                function () {
                    xthf0[0] = new xTableHeaderFixed('fixedheader', window);
                }, false
                );

        xAddEventListener(window, 'load',
                function () {
                    for (var i = 0; i < o_tabla_t.length; i++) {
                        var t = new SortableTable(document.getElementById(o_tabla_t[i]), 1000);
                    }
                }, false
                );

        xAddEventListener(window, 'load',
                function () {
                    for (var i = 0; i < o_tabla_s.length; i++) {
                        var t = new SortableTable(document.getElementById(o_tabla_s[i]), 1000);
                    }
                }, false
                );
    }
}

function agregar_onclick_link(m) {
//    var trt = window.location.pathname;
//    alert("->" + window.location.pathname);
//    var links = document.getElementsByTagName("A");
    var links = document.getElementsByClassName("pagtab")[0].getElementsByTagName("A");
    for (var i = 0; i < links.length; i++) {
        var link = links[i];
        var href = link.href;
        if (m) {
            try {
                link.removeAttribute("target");
            } catch (e) {
                console.log("e:" + e);
            }
        }
        if (href.charAt(href.length - 1) != "#") {
            var href = link.getAttribute("href");
            href = href.indexOf("../") == 0 ? href.substr(3, href.length) : href;
//            console.log("confiugrando atribute target:" + link.getAttribute("target"));
//            if (link.getAttribute("target") == undefined | link.getAttribute("target") == null | link.getAttribute("target") == "") {//obsoleto
            if (link.getAttribute("target") == undefined | link.getAttribute("target") == null) {
                href = href + "&cabecera=0";
            }
            link.setAttribute("href", href);
            if (href.indexOf("wf?") == 0 | href.indexOf("wg?") == 0) {
                link.onclick = function () {
                    var node = this;
                    var tieneFe = node.getAttribute("href").split("&fe_solini=");
                    if (tieneFe.length > 1) {
                        node.setAttribute("href", node.getAttribute("href").replace("&fe_solini=" + tieneFe[1], "&fe_solini=" + new Date().getTime()));
                    } else {
                        node.setAttribute("href", node.getAttribute("href") + "&fe_solini=" + new Date().getTime());
                    }
                }
            } else if (href.indexOf("reportes/paginaEspecial.jsp?") == 0) {
                var nhref = href;
                var parent = link.parentNode;
//                console.log("parent:" + parent + "::" + nhref);
                parent.innerHTML = "[ <a href='" + nhref.replace("reportes/paginaEspecial.jsp?", "doc?ti_docume=E&") + "' target='_blank'>Ver</a> ]"
//                console.log(parent.innerHTML);
            } else if (href.indexOf("doc?") == 0) {
                var nhref = href;
                var parent = link.parentNode;
//                console.log("parent:" + parent + "::" + nhref);
                parent.innerHTML = "[ <a href='" + nhref + "' target='_blank'>Ver</a> ]"
//                console.log(parent.innerHTML);
            }

        }
    }
    var imgs = document.getElementsByClassName("pagtab")[0].getElementsByTagName("IMG");
    for (var i = 0; i < imgs.length; i++) {
        var img = imgs[i];
        var src = img.getAttribute("src");
        src = src.indexOf("../") == 0 ? src.substr(3, src.length) : src;
        img.setAttribute("src", src);
    }
}

/*
 function agregar_onclick_link2() {
 var links = document.getElementsByTagName("A");
 for (var i = 0; i < links.length; i++) {
 var link = links[i];
 var href = link.href;
 console.log("Target::" + link.getAttribute("target") + "<->href::" + link.href);
 if (link.getAttribute("target") == null & link.href.indexOf("wf?") > -1) {
 console.log("se propone cambiarlo::" + link.href.replace("wf?", "wf2?"));
 link.href = link.href.replace("wf?", "wf2?");
 }
 if (href.charAt(href.length - 1) != "#") {
 link.onclick = function () {
 var node = this;
 var tieneFe = node.href.split("&fe_solini=");
 if (tieneFe.length > 1) {
 node.href = node.href.replace("&fe_solini=" + tieneFe[1], "&fe_solini=" + new Date().getTime());
 } else {
 node.href = node.href + "&fe_solini=" + new Date().getTime();
 }
 }
 }
 }
 }
 */


function acomodadorcss() {
    var f = false;
    var ff = navigator.userAgent;
    //alert(">>"+ff);
    var v = ff.search('Firefox');
    if (v > 0) {
        ff = ff.substr(v, ff.length);
        v = ff.search('/');
        var s = ff.substring(v + 1, v + 2);
        if (s < 4) {
            f = true;
        }
    } else {
        v = ff.search('Chrome');
        if (v > 0) {
            //f=true;
        } else {
            v = ff.search('Opera');
            if (v > 0) {
                //f=true;
            } else {
                v = ff.search('MSIE');
                if (v > 0) {
                    f = true;
                }
            }
        }
    }

    if (f) {
        for (var i = 0; i < o_tabla_f.length; i++) {
            var tb = $D.g_ID(o_tabla_f[i]);
            if (tb.className == "tdonline") {
                var body = tb.getElementsByTagName("TBODY")[0];
                for (var w = 0; w < body.rows.length; w++) {
                    var row = body.rows[w];

                    var cell = row.cells[0];
                    if (cell.getAttribute("colspan") == "2") {
                    }
                    else {
                        cell.setAttribute("class", "tdPagina2");
                        cell.className = "tdPagina2";
                        var cell1 = row.cells[1];
                        cell1.setAttribute("class", "tdPagina4");
                        cell1.className = "tdPagina4";
                    }
                    //alert(">"+row.innerHTML);
                }
            }
        }
    }

}

/**
 p = inputs;
 **/
function documentofocus() {
    var q = false;
    var p = $D.g_TNAME("input");
    var i;
    for (i = 0; i < p.length; i++) {
        if (p[i].getAttribute("type") == "text") {
            p[i].focus();
            q = true;
            i = p.length - 1;
            break;
        }
    }
    if (!q) {
        p = $D.g_TNAME("textarea");
        for (i = 0; i < p.length; i++) {
            p[i].focus();
            i = p.length - 1;
            break;
        }
    }
}


/**
 * BLOCKPANEL & POPUPS
 * */
function bp1_btn1() {
    $D.g_ID('pagreg').src = $D.base() + "/workflow2/wfp.jsp";
    $D.g_ID('BP1').style.display = 'none';
    $D.unlock();
    return false;
}

function confirmacion_boton_no() {
    $D.g_ID('confirmacion').style.display = 'none';
    $D.g_ID('modalDialogPageBackground').style.display = 'none';
    $D.unlock();
    return false;
}

/* POPUP de modulos */

var popup_resize = function () {
    $D.getElementById("popup_load").style.display = "none";
    $D.getElementById("popup_body").style.display = "block";
    var pup = $D.getElementById("popup_body").contentWindow.document;
    var idata = pup.getElementsByClassName("pagtab")[0].offsetWidth;
    $D.g_ID('popup').style.width = (parseInt(idata) + 20) + "px";
};

var popup_resizeB = function () {
    $D.getElementById("popup_load").style.display = "none";
    $D.getElementById("popup_body").style.display = "block";
    console.log("this:" + this);
    $D.g_ID('popup').style.width = "98%";
};

function parent_close_popup(msg) {
    var L = $D.g_ID($D.g_ID("popup_body").getAttribute("ele"));
    if (msg.toString() == "") {

        if (L.tagName == "LABEL") {
            L.innerHTML = msg;
        } else if (l.tagName == "INPUT") {
            L.value = msg;
        }

    } else if (msg.toString() == "undefined") {

    } else {
        if (L.tagName == "LABEL") {
            L.innerHTML = msg.co_conpar_2;
            L.setAttribute("valpag", msg.co_conpar_1);
            L.setAttribute("v", msg.co_conpar_1)
        } else if (L.tagName == "INPUT") {
            L.value = msg.co_conpar_1;
        }
    }

    $D.g_ID("popup_body").onload = null;
    $D.g_ID("popup_body").src = "";
    $D.g_ID('popup').style.display = "none";
    $D.unlock();
}

function parent_close_popup_georeferencia_10(msg) {
    var E = $D.g_ID("popup_body").getAttribute("ele");
    var L = $D.g_ID('v' + E);
    console.log("msg:" + msg);
    if (msg.toString() == "") {
    } else if (msg.toString() == "undefined") {
    } else {
        if (L.value != msg) {
            L.value = msg;
            var lat = msg.split("/")[0].trim();
            var lng = msg.split("/")[1].trim();
            $D.g_ID("i" + E).src = "http://maps.googleapis.com/maps/api/staticmap?zoom=15&size=370x120&center=" + lat + "," + lng + "&markers=color:red|" + lat + "," + lng;
        }
    }

    $D.g_ID("popup_body").onload = null;
    $D.g_ID("popup_body").src = "";
    $D.g_ID('popup').style.display = "none";
    $D.unlock();
}

function parent_close_popup_georeferencia_39(lat, lng) {
    var E = $D.g_ID("popup_body").getAttribute("ele");
    console.log("e:[" + options_gson['geo' + E].no_latitud + "]?[" + lat + "],[" + options_gson['geo' + E].no_longitud + "][" + lng + "]");
    if (options_gson['geo' + E].no_latitud != lat | options_gson['geo' + E].no_longitud != lng) {
        options_gson['geo' + E].no_latitud = lat;
        options_gson['geo' + E].no_longitud = lng;
        $D.g_ID(E).value = JSON.stringify(options_gson['geo' + E]);
        $D.g_ID("t" + E).innerHTML = "Ver / Editar";
        $D.g_ID("i" + E).src = "http://maps.googleapis.com/maps/api/staticmap?zoom=15&size=370x120&center=" + lat + "," + lng + "&markers=color:red|" + lat + "," + lng;
    }

    $D.g_ID("popup_body").onload = null;
    $D.g_ID("popup_body").src = "";
    $D.g_ID('popup').style.display = "none";
    $D.unlock();
}